package com.wanmei.controller.product;

import com.wanmei.commons.ResultUtils;
import com.wanmei.pojo.Category;
import com.wanmei.pojo.Result;
import com.wanmei.service.Impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("product")
public class CategoryAddController {
    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 添加商品分类
     *
     * @return
     */
    @RequestMapping("categoryAddView")
    public String categoryAddView() {
        return "/product/categoryAdd";
    }

    /**
     * 添加分类接口
     *
     * @param category
     * @return
     */
    @RequestMapping(value = "addCategory",method = RequestMethod.POST)
    @ResponseBody
    public Object addCategory(@RequestBody Category category) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format.format(date);
        category.setAddtime(nowTime);
        if(category.getParentid() == null) {
            category.setParentid(0);
        }
        int result = categoryService.insert(category);
        if (result == 0) {
            return ResultUtils.fail("添加失败", null, 0);
        }
        return ResultUtils.success("添加成功", null, 1);
    }
}
